﻿using System;
using CoreEx.Services;
using LinFu.IoC;
namespace CoreEx.Common.Logging
{
    /// <summary>
    /// Extends the <see cref="object"/> class and adds logging capabilities 
    /// to any object instance.
    /// </summary>
    public static class ObjectExtensions
    {        
        private static ILoggerFactory _loggerFactory;
        

        static ObjectExtensions()
        {            
            _loggerFactory = ServiceLocator.Container.GetService<ILoggerFactory>();
        }

        /// <summary>
        /// Logs a debug message.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="message">The message to log.</param>        
        public static void LogDebug<T>(this T target, string message)
        {
            GetLogger(typeof(T)).LogDebug(message);
        }

        /// <summary>
        /// Logs a debug message.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="message">The message to log.</param>        
        /// <param name="exception">An <see cref="Exception"/> to be logged with the message.</param>
        public static void LogDebug<T>(this T target, string message, Exception exception)
        {
            GetLogger(typeof(T)).LogDebug(message);
        }

        /// <summary>
        /// Logs an info message.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="message">The message to log.</param>        
        public static void LogInfo<T>(this T target, string message)
        {
            GetLogger(typeof(T)).LogDebug(message);
        }

        /// <summary>
        /// Logs an info message.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="message">The message to log.</param>        
        /// <param name="exception">An <see cref="Exception"/> to be logged with the message.</param>
        public static void LogInfo<T>(this T target, string message, Exception exception)
        {
            GetLogger(typeof(T)).LogDebug(message);
        }

        /// <summary>
        /// Logs a warning message.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="message">The message to log.</param>                
        public static void LogWarning<T>(this T target, string message)
        {
            GetLogger(typeof(T)).LogWarning(message);
        }

        /// <summary>
        /// Logs a warning message.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="message">The message to log.</param>        
        /// <param name="exception">An <see cref="Exception"/> to be logged with the message.</param>
        public static void LogWarning<T>(this T target, string message, Exception exception)
        {
            GetLogger(typeof(T)).LogWarning(message);
        }

        /// <summary>
        /// Logs an error message.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="message">The message to log.</param>                
        public static void LogError<T>(this T target, string message)
        {
            GetLogger(typeof(T)).LogError(message);
        }

        /// <summary>
        /// Logs an error message.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="message">The message to log.</param>        
        /// <param name="exception">An <see cref="Exception"/> to be logged with the message.</param>
        public static void LogError<T>(this T target, string message, Exception exception)
        {
            GetLogger(typeof(T)).LogError(message);
        }

        /// <summary>
        /// Logs a fatal message.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="message">The message to log.</param>                
        public static void LogFatal<T>(this T target, string message)
        {
            GetLogger(typeof(T)).LogFatal(message);
        }

        /// <summary>
        /// Logs a fatal message.
        /// </summary>
        /// <param name="target">The target instance.</param>
        /// <param name="message">The message to log.</param>        
        /// <param name="exception">An <see cref="Exception"/> to be logged with the message.</param>
        public static void LogFatal<T>(this T target, string message, Exception exception)
        {
            GetLogger(typeof(T)).LogFatal(message);
        }

        private static ILogger GetLogger(Type type)
        {
            return _loggerFactory.GetLogger(type.FullName);
        }

    }
}
